//将普通列表转换为树结构的列表
export const listToTreeList=(list,id="id",parentId="parentId")=>{
  if (!list || !list.length) {
    return []
  }
  let treeListMap = {};
  for (let item of list) {
    treeListMap[item[id]] = item
  }
  for (let i = 0; i < list.length; i++) {
    if (list[i][parentId] && treeListMap[list[i][parentId]]) {
      if (!treeListMap[list[i][parentId]].children) {
        treeListMap[list[i][parentId]].children = []
      }
      treeListMap[list[i][parentId]].children.push(list[i]);
      list.splice(i, 1);
      i--
    }
  }
  return list
}
//递归找到一个标识的数据 数组,字段名,值
const findInRecursive = (objList, objkey, value) => {
  const obj = objList.find(i => i[objkey] == value)
  if (obj != undefined) {
    return obj
  } else {
    let ac = undefined
    objList.map(i => {
      if (ac == undefined && i.children) {
        ac = findInRecursive(i.children, objkey, value)
      }
    })
    return ac
  }
}

//递归查找一个对象,获取这个对象的树,形成一个数组
const findInRecursiveList = (objList, objkey, value) => {
  let findList = []
  const func = (objList, objkey, value) => {
    //查找一个
    const f = findInRecursive(objList, objkey, value)
    if (f) {
      findList.push(f)
      //设置顶级的标志ID
      if (f.parentId != 1) {
        func(objList, objkey, f.parentId)
      }
    }
  }
  func(objList, objkey, value)
  return findList
}

//删除树中的空值undefined
const treeDeleteUndefined = (treeObjList) => {
  const func = (ls) => {
    if (ls == undefined || ls == null) {
      return
    }
    ls.map((item) => {
      if (item) {
        if (Object.prototype.toString.call(item.children) === '[object Array]') {
          const tList = item.children.filter(i => i != undefined);
          item.children = tList
          func(item.children)
        }
      }
    })
  }
  func(treeObjList)
  return treeObjList.filter(i => i != undefined);
}

export const getRecursive = (list, objList, objkey, listkey) => {

  //深度克隆对象
  const deepClone = (obj) => {
    const getObjClass = (obj) => {
      let result = Object.prototype.toString.call(obj).slice(8, -1);
      if (result === 'Null') {
        return 'Null';
      } else if (result === 'Undefined') {
        return 'Undefined';
      } else {
        return result;
      }
    }
    let result, objClass = getObjClass(obj);
    if (objClass === 'Object') {
      result = {};
    } else if (objClass === 'Array') {
      result = [];
    } else {
      return obj; // 如果是其他数据类型不复制，直接将数据返回
    }
    // 遍历目标对象
    for (let key in obj) {
      let value = obj[key];
      if (getObjClass(value) === "Object" || 'Array') {
        result[key] = deepClone(value);
      } else {
        result[key] = obj[key];
      }
    }
    return result;
  }

  let tObjList = deepClone(objList)
  //获取全部的数组
  let retList = []
  list.map(item => retList.push(findInRecursiveList(objList, objkey, item[listkey])))
  const chkFindInRetList = (record) => {
    //console.log("===>record===>",record);

    if (record == undefined || record == null) {
      return false
      return
    }
    let rt = false
    // console.log("===>retList===>",retList);//没错
    retList.map(i => {
      i.map(iv => {
        if (record[objkey] == iv[objkey]) {
          rt = true
        }
      })
    })
    return rt
  }
  //遍历树的每个节点,如果没有就删除
  const findInObjList = (findObjList) => {
    if (findObjList == undefined || findObjList == null) {
      return
    }
    findObjList.map((v, i) => {
      if (chkFindInRetList(v)) {
        findInObjList(v.children)
      } else {
        findObjList[i] = undefined
      }
    })
  }
  findInObjList(tObjList)
  return treeDeleteUndefined(tObjList)
}
/*

//使用实例
const aaa = [
    {
        name: "a51",
        children: [
            {
                name: "a63",
                children: null,
            }
        ]
    },
    {
        name: "a1",
        children: [
            {
                name: "a63s",
                is_check: true,
                children: [
                    {
                        name: "n33",
                    }
                ]
            },
        ]
    },
    {
        name: "a5",
        children: [
            {
                name: "a6",
                children: [
                    {
                        name: "n3",
                        is_check: true
                    },
                    {
                        name: "n33w",
                        is_check: true
                    },
                ]
            },
        ]
    },

    {
        name: "a55",
        children: [
            {
                name: "a63",
                children: [
                    {
                        name: "n36",
                        is_check: true
                    },
                    {
                        name: "n37",
                        is_check: true
                    },
                    {
                        name: "n38",
                        is_check: true
                    }
                ]
            },
        ]
    },
]



const bbb = [
    {name_v: "n36"},
    {name_v: "n37"},
    {name_v: "n38"},
    {name_v: "n33"},
]


//在一个递归的对象中根据特有的字段属性查找到这个节点
console.log("===>findInRecursive===>", JSON.stringify(findInRecursive(aaa, "name","n37")));
//===>findInRecursive===> {"name":"n37","is_check":true}

//在一个递归的对象中根据特有的字段属性查找到这个节点,并且找出这个节点的父逻辑,形成一个数组
console.log("===>findInRecursiveList===>", JSON.stringify(findInRecursiveList(aaa, "name","n37")));
//===>findInRecursiveList===> [{"name":"n37","is_check":true},{"name":"a63","children":null},{"name":"a55","children":null}]

//在一组特有ID的数据中,去寻找这个树中符合这个条件的树,包括父节点,形成一个包裹关系,children的树数据
console.log("===>getRecursive===>", JSON.stringify(getRecursive(bbb, aaa, "name","name_v")));
//===>getRecursive===> [{"name":"a55","children":[{"name":"a63","children":[[{"name":"n36","is_check":true},{"name":"n37","is_check":true},{"name":"n38","is_check":true}]]}]},{"name":"a5","children":[{"name":"a6","children":[[{"name":"n33w","is_check":true}]]}]}]

//在一组特有ID的数据中,去寻找这个树中符合这个条件的树,包括父节点,形成一个包裹关系,children的树数据
console.log("===>getRecursive2===>", JSON.stringify(getRecursive2(bbb, aaa, "name","name_v")));
//===>getRecursive2===> [{"name":"a55","children":[{"name":"a63","children":[[{"name":"n36","is_check":true},{"name":"n37","is_check":true},{"name":"n38","is_check":true}]]}]},{"name":"a5","children":[{"name":"a6","children":[[{"name":"n33w","is_check":true}]]}]}]
*/




/*
//根据一个数组找,在递归的对象中找出路径的数据集合,有漏洞,会产生重复项
const getRecursive2 = (list, objList,objkey,listkey) => {

    let retList = []
    list.map(item => retList.push(findInRecursiveList(objList, objkey, item[listkey])))

    //深度克隆对象
    const deepClone=(obj)=>{
        const getObjClass=(obj)=>{
            let result = Object.prototype.toString.call(obj).slice(8, -1);
            if(result === 'Null'){
                return 'Null';
            }else if(result === 'Undefined'){
                return 'Undefined';
            }else {
                return result;
            }
        }
        let result, objClass = getObjClass(obj);
        if(objClass === 'Object'){
            result = {};
        }else if(objClass === 'Array'){
            result = [];
        }else {
            return obj; // 如果是其他数据类型不复制，直接将数据返回
        }
        // 遍历目标对象
        for(let key in obj){
            let value = obj[key];
            if(getObjClass(value) === "Object" || 'Array'){
                result[key] = deepClone(value);
            }else {
                result[key] = obj[key];
            }
        }
        return result;
    }

    const findList = (l1) => {
        const code = JSON.stringify(l1.filter((i, index) => index != 0))
        let fList = retList.filter(item => item != undefined).filter(item => {
            if (JSON.stringify(item.filter((i, index) => index != 0)) == code) {
                return true
            }
        })
        //把数组变成结构
        let tList=deepClone([fList.map(item => item[0]), ...l1.filter((i, index) => index != 0)])
        for (let i = 0; i < tList.length; i++) {
            if ((i + 1) < tList.length) {
                if (tList[i + 1].children == null) {
                    tList[i + 1].children = []
                }
                if(Object.prototype.toString.call(tList[i]) === '[object Array]') {
                    tList[i].map(im=>{
                        tList[i + 1].children.push(im)
                    })
                }else{
                    tList[i + 1].children.push(tList[i])
                }
            }
        }
        //===>334===> {"name":"a55","children":[{"name":"a63","children":[[{"name":"n36","is_check":true},{"name":"n37","is_check":true},{"name":"n38","is_check":true}]]}]}
        //console.log("===>334===>", JSON.stringify(tList[tList.length - 1]));
        return tList[tList.length - 1]
    }
    //进行去重复数组
    let jList=[]
    retList.map(i=>{
        const fl=findList(i)
        if(!jList.find(iv=>JSON.stringify(iv)==JSON.stringify(fl))){
            jList.push(fl)
        }
    })
    return jList
}

*/
